<template>
  <!--搜索-->
  <!--    搜索框-->
  <div style="padding: 10px 0">
    <el-input
      style="width: 200px"
      placeholder="请输入身份证号"
      clearable
      v-model="searchid"></el-input>
    <el-input
      style="width: 150px; margin-left: 15px;"
      placeholder="请输入疫苗编号"
      clearable
      v-model="vaccinid"
    ></el-input>
    
    <el-button
      type="primary"
      style="margin-left: 15px"
      @click="getinoculationAll"
    >
      <el-icon style="vertical-align: middle">
        <search />
      </el-icon>
      <span style="vertical-align: middle"> 搜索 </span>
    </el-button>
    <!-- 添加按钮 -->
    <el-button type="primary" style="margin-left: 15px" @click="addinoculation">
      <el-icon style="margin-right: 5px"><CirclePlusFilled /></el-icon>
      添加
    </el-button>
  </div>
  <!--接种列表-->
  <el-table :data="tableData" style="width: 100%" height="450">
    <el-table-column fixed prop="id" label="序号" width="80" />
    <el-table-column prop="userid" label="身份证号" width="180" />
    <el-table-column prop="vaccinid" label="疫苗编号" width="140" />
    <el-table-column prop="jabsum" label="接种针数" width="80" align="center"/>
    <el-table-column prop="reservationid" label="预约编号" width="80" align="center"/>
    <el-table-column prop="address" label="接种地点" width="280" align="center"/>
    <el-table-column prop="jabtime" label="接种时间" width="180" align="center"/>

    <el-table-column fixed="right" label="操作" width="140">
      <template #default="scope">
        <el-button
          type="warning"
          size="small"
          @click="inoculationedit(scope.row)"
          >编辑</el-button
        >
        <el-button
          type="danger"
          size="small"
          @click="delinoculation(scope.row.id)"
          >删除</el-button
        >
      </template>
    </el-table-column>
  </el-table>
  <!--分页-->
  <div style="padding: 10px 0">
    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      v-model:currentPage="pageNum"
      :page-sizes="[10, 20, 30, 50]"
      v-model:page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
  <!-- 编辑或者添加的弹窗 -->
  <el-dialog title="用户信息" v-model="dialogFormVisible" width="50%">
    <el-form :model="form" size="default" :rules="rules" ref="form">
      <el-row>
      <el-col :span="8">
      <el-form-item label="序号" label-width="100px">
        <el-input v-model="form.id" autocomplete="off" disabled></el-input>
      </el-form-item></el-col>
      <el-col :span="14">
      <el-form-item label="身份证号" label-width="100px" prop="userid">
        <el-input v-model="form.userid" autocomplete="off"></el-input>
      </el-form-item></el-col> 
    </el-row>
    <el-row>
        <el-col :span="12">
          <el-form-item label="疫苗名称" label-width="100px" prop="vaccinid">
            <el-select v-model="form.vaccinid" placeholder="请选择">
              <el-option
                v-for="item in vaccineData"
                :key="item.value"
                :label="item.vname"
                :value="item.createid"
              >
                <span style="float: left">{{ item.createid }}</span>
                <span
                  style="
                    float: right;
                    color: var(--el-text-color-secondary);
                    font-size: 13px;
                  "
                  >{{ item.vname }}</span
                >
              </el-option>
            </el-select>
          </el-form-item></el-col
        >
        <el-col :span="8">
          <el-form-item label="接种针数" label-width="100px" prop="jabnum">
            <el-select v-model="form.jabsum">
              <el-option label="第一针 " value="1" />
              <el-option label="第二针 " value="2" />
              <el-option label="第三针 " value="3" />
            </el-select> </el-form-item
        ></el-col>
      </el-row>
    <el-row>
      <el-col :span="18">
      <el-form-item label="预约序号" label-width="100px" prop="reservationid">
        <el-input v-model="form.reservationid" autocomplete="off"></el-input>
      </el-form-item>
      </el-col></el-row>
      <el-row>
        <el-col :span="20">
          <el-form-item label="接种地址" label-width="100px" prop="adress">
            <el-select v-model="form.address" placeholder="请选择">
              <el-option
                v-for="item in addressData"
                :key="item.value"
                :label="item.address"
                :value="item.fromhospital"
              >
                <span style="float: left">{{ item.address }}</span>
                <span
                  style="
                    float: right;
                    color: var(--el-text-color-secondary);
                    font-size: 13px;
                  "
                  >{{ item.fromhospital }}</span
                >
              </el-option>
            </el-select> </el-form-item></el-col
      ></el-row>
      <el-row>
      <el-col :span="8"
          ><el-form-item label="接种时间" label-width="100px" prop="jabtime">
            <el-date-picker
              v-model="form.jabtime"
              type="datetime"
              placeholder="选择日期和时间"
              :size="size" /></el-form-item
        ></el-col>
          </el-row>
    </el-form>

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogFormVisible = false"> 取消</el-button>
        <el-button type="primary" @click="saveData('form')"> 确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import { ElMessage, ElMessageBox } from "element-plus";
export default {
name: "Inoculation",
data() {
  return {
    tableData: [],
    vaccineData:[],
    addressData:[],
    total: 0, //数据总条数
    pageNum: 1, //当前页
    pageSize: 10, //页大小
    searchid: "", //搜索证号
    vaccinid: "", //疫苗编号
    dialogFormVisible: false,
    form: {},
    rules: {
      username: [
        { required: true, message: "请输入用户名", trigger: "blur" },
      ],
      avatar: [
        { required: true, message: "请输入头像地址", trigger: "blur" },
      ],
    },
  };
},
methods: {
  handleSizeChange(val) {
    //页大小改变
    this.pageSize = val;
    this.getinoculationAll();
  },
  handleCurrentChange(val) {
    //当前页改变
    this.pageNum = val;
    this.getinoculationAll();
  },
  //分页查询
  getinoculationAll() {
    this.$http
      .get(
        "/inoculations/inoculation/pageall",
        {
          params: {
            pageNum: this.pageNum,
            pageSize: this.pageSize,
            userid: this.searchid,
            vaccinid: this.vaccinid,
          },
        }
      )
      .then((res) => {
        if (res.statusCode == "200") {
          this.total = res.data.total;
          this.tableData = res.data.records;
        }
      })
      .catch(() => {
        ElMessage.error("数据加载失败,请刷新！");
      });
  },
   //分页所有疫苗编号和名称查询
   getvoccinAll() {
      this.$http
        .get("/inoculations/vaccin/pageall", {
          params: {
            pageNum: 1,
            pageSize: 20,
          },
        })
        .then((res) => {
          if (res.statusCode == "200") {
            this.vaccineData = res.data.records;
          }
        })
        .catch(() => {
          ElMessage.error("数据加载失败,请刷新！");
        });
    },
    //分页所有地址名称查询
    getadressAll() {
      this.$http
        .get("/inoculations/address/pageall", {
          params: {
            pageNum: 1,
            pageSize: 20,
          },
        })
        .then((res) => {
          if (res.statusCode == "200") {
            this.addressData = res.data.records;
          }
        })
        .catch(() => {
          ElMessage.error("数据加载失败,请刷新！");
        });
    },
  // 编辑
  inoculationedit(row) {
    this.getadressAll();
    this.getvoccinAll();
    this.form = row;
    this.dialogFormVisible = true;
  },
  // 添加
  addinoculation() {
    this.getadressAll();
    this.getvoccinAll();
    this.form = {};
    this.dialogFormVisible = true;
  },
  //用于数据的添加和更新
  saveData() {
    this.$refs.form.validate((valid) => {
      if (valid) {
        this.$http
          .post("/inoculations/inoculation/save", this.form)
          .then((res) => {
            if (res.statusCode == "200") {
              ElMessage({
                message: "成功！",
                type: "success",
              });
              this.dialogFormVisible = false;
              this.getinoculationAll();
            }
          })
          .catch(() => {
            ElMessage.error("失败！");
          });
      } else {
        ElMessage({
          message: "请填写！",
          type: "warning",
        });
        return false;
      }
    });
  },

  // 删除
  delinoculation(id) {
    ElMessageBox.confirm("是否删除选中数据？", "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    })
      .then(() => {
        let token = localStorage.getItem("token");
        this.$http
          .delete("/inoculations/inoculation/del/" + id, token)
          .then((res) => {
            if (res.statusCode == "200") {
              ElMessage({
                message: "删除成功！",
                type: "success",
              });
              this.getinoculationAll();
            }
          })
          .catch(() => {
            ElMessage.error("删除失败！");
          });
      })
      .catch(() => {
        ElMessage({
          type: "info",
          message: "已取消！",
        });
      });
  },
},
created() {
  this.getinoculationAll();
},
};
</script>

<style scoped>
.el-button + span {
margin-left: 10px;
}
</style>
